package com.hsq.androidproject.ui;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.weight.DragImageView;

public class ImageActivity extends FragmentActivity {
    private RelativeLayout rl_layout;

    DragImageView mDragImageView;
    private int window_width, window_height;// 控件宽度
    private int state_height;// 状态栏的高度

    //观察者模式
    private ViewTreeObserver viewTreeObserver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.activity_image);
        rl_layout = (RelativeLayout) findViewById(R.id.rl_layout);
        Intent intent = getIntent();
        final String path = intent.getStringExtra("bigImg");

        mDragImageView = (DragImageView) findViewById(R.id.mDragView);
        mDragImageView.setmActivity(this, path);
        /** 获取可见区域高度 **/
        WindowManager manager = getWindowManager();
        window_width = manager.getDefaultDisplay().getWidth();
        window_height = manager.getDefaultDisplay().getHeight();

        Glide.with(ImageActivity.this)
                .load(path)
                .into(mDragImageView);


        viewTreeObserver = mDragImageView.getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (state_height == 0) {
                    // 获取状况栏高度
                    Rect frame = new Rect();
                    getWindow().getDecorView()
                            .getWindowVisibleDisplayFrame(frame);
                    state_height = frame.top;
                    mDragImageView.setScreen_H(window_height - state_height);
                    mDragImageView.setScreen_W(window_width);
                }
            }
        });
    }
}

